<?php

/**
 * 业务员管理
 */

namespace Admin\Controller\Saleman;

use Admin\Common\SalemanHelper;
use Think\Upload;
use Admin\Controller\Saleman\BaseController;

class OrderController extends BaseController {

    /**
     * 列表
     */
    public function lists() {
        $where = array();
        $id = $_REQUEST['order_id'];
        $state = $_REQUEST['state'];
        //点击分页查询
        if(!isset($state)&&!empty($_REQUEST['p'])){
        		$state = session('state');
        }elseif(!isset($state)&&empty($_REQUEST['p'])){ //刷新页面
        		$state＝0;
        }else{
       	 	session('state',$state);
        }
       
        if (!empty($id)) {
            $where ['id'] = array('like', '%' . $id . '%');
        }
        if (!empty($state)) {
        		if($state==6){  //诊所订单
        			$where ['type'] = array(0);
        		}
       	 	
        }
        //查询列表信息
        $salemanOrderModel = M('saleman_order');
        
        
        //查询数据
        $order = array(
            'add_time' => 'desc'
        );
        //得到分页信息
        if($state==0||$state==6){
        		$count = $salemanOrderModel->where($where)->count();
        		$pageTool = new \Think\Page($count, 10); //分页
        		$list = $salemanOrderModel->where($where)->order($order)->limit($pageTool->firstRow, $pageTool->listRows)->select();
        }else{
        		$count=$this->orderCount($state); 
       	 	$pageTool = new \Think\Page($count, 10); //分页
	        	$list=$this->orderCondition($state, $pageTool);
        }
		//设置分页信息
		$page_config = C('PAGE_CONFIG');
		foreach ($page_config as $k => $v) {
			$pageTool->setConfig($k, $v);
		}
		$pageHtml = $pageTool->show();
        //组装数据
        if (check_array($list)) {
            $salemanIdList = array();
            $clinicIdList = array();
            foreach ($list as $key => $value) {
                if ($value['type'] == 0) {
                    if (!in_array($value['send_id'], $clinicIdList)) {
                        $clinicIdList[] = $value['send_id'];
                    }
                } else {
                    if (!in_array($value['send_id'], $salemanIdList)) {
                        $salemanIdList[] = $value['send_id'];
                    }
                }

                if (!in_array($value['saleman_id'], $salemanIdList)) {
                    $salemanIdList[] = $value['saleman_id'];
                }
            }

            //查询诊所集合
            $clinicModel = M('clinic');
            $where = array(
                'id' => array('in', $clinicIdList)
            );
            $clinicList = $clinicModel->where($where)->select();
            $clinicList = set_array_key($clinicList, 'id');

            //查询业务员集合
            $salemanModel = M('saleman');
            $where = array(
                'id' => array('in', $salemanIdList)
            );
            $salemanList = $salemanModel->where($where)->select();
            $salemanList = set_array_key($salemanList, 'id');

            //组装数据
            foreach ($list as $key => $value) {
                if ($value['type'] == 0) {
                    $list[$key]['send_name'] = isset($clinicList[$value['send_id']]) ? $clinicList[$value['send_id']]['clinic_name'] : '-';
                } else {
                    $list[$key]['send_name'] = isset($salemanList[$value['send_id']]) ? $salemanList[$value['send_id']]['real_name'] : '-';
                }
                $list[$key]['accept_name'] = isset($salemanList[$value['saleman_id']]) ? $salemanList[$value['saleman_id']]['real_name'] : '-';
            }
        }
        //查询条件数组
        $condition = array(
        		'0'=>'全部订单',
        		'2'=>'省总订单',
        		'3'=>'地总订单',
        		'4'=>'县总订单',
        		'5'=>'业务员订单',
        		'6'=>'诊所订单',
        );
        $this->assign(array(
            'page' => $pageHtml,
            'list' => $list,
            '_total' => $count,
        		'state'=>$condition,
        		'currentState'=>$state
        ));
        $this->display();
    }

    /**
     * 编辑数据
     */
    public function edit() {
        if (IS_POST) {



            responseJson(1, '业务员修改成功');
            exit;
        }

        //查询订单
        $salemanOrderModel = M('saleman_order');
        $id = $_GET['id'];
        $where = array(
            'id' => array('eq', $id)
        );
        $order = $salemanOrderModel->where($where)->find();

        //查询订单详情
        $salemanOrderDetail = M('saleman_order_detail');
        $where = array(
            'order_id' => array('eq', $id)
        );
        $orderDetailList = $salemanOrderDetail->where($where)->select();

        //查询发起人和接收人
        $clinicIdList = array();
        $salemanIdList = array();
        if ($order['type'] == 0) {
            $clinicId = $order['send_id'];

            //查询诊所集合
            $clinicModel = M('clinic');
            $where = array(
                'id' => array('eq', $clinicId)
            );
            $clinic = $clinicModel->where($where)->find();
            $order['send_name'] = check_array($clinic) ? $clinic['clinic_name'] : '-';
        } else {
            if (!in_array($order['send_id'], $salemanIdList)) {
                $salemanIdList[] = $order['send_id'];
            }
        }

        if (!in_array($order['saleman_id'], $salemanIdList)) {
            $salemanIdList[] = $order['saleman_id'];
        }

        //查询业务员集合
        $salemanModel = M('saleman');
        $where = array(
            'id' => array('in', $salemanIdList)
        );
        $salemanList = $salemanModel->where($where)->select();
        $salemanList = set_array_key($salemanList, 'id');
        if (!isset($order['send_name'])) {
            $order['send_name'] = isset($salemanList[$order['send_id']]) ? $salemanList[$order['send_id']]['real_name'] : '-';
        }
        $order['accept_name'] = isset($salemanList[$order['saleman_id']]) ? $salemanList[$order['saleman_id']]['real_name'] : '-';

        $this->assign(
                array(
                    'order' => $order,
                    'orderDetailList' => $orderDetailList
                )
        );
        $this->display();
    }
    
    /**
     * 订单帅选条件[业务员]
     */
    public function orderCondition($state,$pageTool){
    		if(!empty($state)){
    			$list= M('saleman_order')->join('__SALEMAN__ ON __SALEMAN__.id =__SALEMAN_ORDER__.send_id')->where(array('type'=>1,'role_id'=>$state))->limit($pageTool->firstRow, $pageTool->listRows)->select();
    		}
    		return $list;
   	 	
    }
    
    public function orderCount($state){
    		return  M('saleman_order')->join('__SALEMAN__ ON __SALEMAN__.id =__SALEMAN_ORDER__.send_id')->where(array('type'=>1,'role_id'=>$state))->count();
    }

}
